package com.tensquare.notice.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.baomidou.mybatisplus.plugins.Page;

import com.tensquare.entity.Result;
import com.tensquare.notice.dao.NoticeDao;
import com.tensquare.notice.dao.NoticeFreshDao;
import com.tensquare.notice.feign.ArticleFeignClient;
import com.tensquare.notice.feign.UserFeignClient;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import com.tensquare.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author BAO
 * @version V1.0
 * @Package tensquare.notice.service.impl
 * @date 2020/12/4 22:00
 * @description:
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Autowired
    private NoticeFreshDao noticeFreshDao;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ArticleFeignClient articleFeignClient;

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    @Override
    public Notice findById(String id) {
        Notice notice = noticeDao.selectById(id);
        getNoticeInfo(notice);
        return notice;
    }
    /**
     * 条件查询并分页
     * @param notice
     * @return
     */
    @Override
    public Page<Notice> findByCondition(Notice notice, Integer current, Integer size) {
        Page<Notice> page = new Page<>(current, size);
        List<Notice> list = noticeDao.selectPage(page, new EntityWrapper<>(notice));
        list.forEach(n-> getNoticeInfo(n));
        page.setRecords(list);
        return page;
    }

    /**
     * 保存
     * @param notice
     * @return
     */
    @Override
    public int save(Notice notice) {
        //雪花算法生成id
        Snowflake snowflake = IdUtil.createSnowflake(0, 1);
        long id = snowflake.nextId();
        notice.setId(String.valueOf(id));

        notice.setState("0");
        notice.setCreatetime(new Date());
        noticeDao.insert(notice);

        NoticeFresh noticeFresh = new NoticeFresh();
        noticeFresh.setUserId(String.valueOf(id));
        noticeFresh.setNoticeId(notice.getReceiverId());
        return noticeFreshDao.insert(noticeFresh);
    }

    /**
     * 根据id更新
     * @param notice
     * @return
     */
    @Override
    public int updateById(Notice notice) {
        return updateById(notice);
    }

    /**
     * 查询消息相关数据
     * @param notice
     */
    @Override
    public void getNoticeInfo(Notice notice) {
        //获取用户信息
        Result result = userFeignClient.findById(notice.getOperatorId());
        HashMap data = (HashMap)result.getData();
        notice.setOperatorName(data.get("nickname").toString());

        //获取文章信息
        if ("article".equals(notice.getTargetType())){
            Result articleResult  = articleFeignClient.findByProblemId(notice.getTargetId());
            HashMap resultData = (HashMap)articleResult.getData();
            notice.setTargetName(resultData.get("title").toString());
        }
    }


}
